<?php
require_once 'CMS/Util.php';
require_once 'CMS/User.php';
require_once 'CMS/Mvc/BaseController.php';
require_once 'CMS/Mvc/BaseAdminController.php';
require_once 'CMS/AdminCommon.php';
require_once 'CMS/Language.php';

//Người tạo : Lại Ngọc Thái Châu - Team 0
//Module 	: PageManager
//Chức năng	: Quản lý các content của các page

class Admin_PageController extends BaseAdminController {
		
	public function init(){
		parent::init();
		$this->_helper->layout()->setLayout('layoutadmin');
		$user = new User();
		$user->init();
		$user->check_login();
		$user->permission();
	}
	
	public function indexAction()
	{
		$dateInt = new Util();
		$tbCurrentPage = new Admin_Model_CurrentPage();
		$currentpage = $tbCurrentPage->selectAllPage();
		$i=0;
		
		$user = new User();
		$user->init();
		$user->check_login();
		$hasmodify = $user->modifiy("admin/page");
		$this->view->modify = $hasmodify;
		foreach($currentpage as $page)
		{
			$des_lang=unserialize($page['description']);
			$title_lang=unserialize($page['title']);
			$page['title']=$title_lang['Default'];
			$page['description']=$des_lang['Default'];
			$array[$i] = array(
				"name" => $page['title'],
				"content" => $page['description'],
				"updated" => $dateInt->makeDays($page['updated_date']),
				"link_edit_module" => "/admin/editpage?page=".$page['md5_code'],
				"link_delete" => "/admin/page/delete?page=".$page['md5_code'],
				"link_edit" => "/admin/page/post?page=".$page['md5_code'],
				"default" => $page['default']
				);
			$i++;
			
		}
		$this->view->currentpage = $array;
	}
	
	public function editAction(){
		$this->_helper->layout()->disableLayout();
		if(isset($_GET['page']))
		{
			$tbCurrentPage = new Admin_Model_CurrentPage();
			$tbContentModuleToPage = new Admin_Model_ContentModuleToPage();
			$page = $tbCurrentPage->selectPageByCode($_GET['page']);
			if((count($page)>0)&&($page[0]['default']==0))
			{
				$this->view->page = $_GET['page'];
				if((isset($_POST))&&(count($_POST)>0)&&(isset($_GET['action']))&&($_GET['action']=="edit"))
				{
					if(!isset($_POST['title'])||(strlen($_POST['title'])<5))
					{
						$this->view->error = 2;
					}
					if($_GET['page'] != md5($_POST['code']))
					{
						if(isset($_POST['code'])&&(strlen($_POST['code'])>5))
						{
							$md5 = md5($_POST['code']);
							$page_temp = $tbCurrentPage->selectPageByCode($md5);
							if(count($page_temp)>0)$this->view->error = 3;
						}
						else
						{
							$this->view->error = 4;
						}
					}
					
					if(!isset($this->view->error))
					{
						$data = array(
									'title' => $_POST['title'],
									'page_code'=>$_POST['code'],
									'md5_code' => md5($_POST['code']),
									'link'	=> $_SERVER['HTTP_HOST'].'/newpage?page='.md5($_POST['code']),
									'updated_date'=>Util::get_time_now()
									);
						$result = $tbCurrentPage->updatePropertiesCurrentPage($page[0]['page_code'],$data);
						$data_2 = array(
										'page_code'=>$_POST['code'],		
									);
						$result = $tbContentModuleToPage->updatePageCodeByCode($page[0]['page_code'],$data_2);
						$this->view->error = $result;
					}
				}
				
				if(isset($_POST["title"]))
				{
					$this->view->title = $_POST["title"];
				}
				else 
					if(isset($page[0]['title']))
					{
						$this->view->title = $page[0]['title'];
					}
					else 
					{
						$this->view->title = "";
					}
					
				if(isset($_POST["code"]))
				{
					$this->view->code = $_POST["code"];
				}
				else 
					if(isset($page[0]['page_code']))
					{
						$this->view->code = $page[0]['page_code'];
					}
					else 
					{
						$this->view->code = "";
					}
			}
			else
			{
				$this->view->error = 5;
			}
		}
		else
		{
			$this->view->error = 5;
		}
	}
	
	public function postAction(){
		$this->_helper->layout()->disableLayout();
		$tbCurrentPage = new Admin_Model_CurrentPage();
		$tbLanguages = new Language_Model_Language();
		
		$Languages = $tbLanguages->getAllLanguages();
		$this->view->languages = $Languages;
		if(isset($_GET['page']))
		{
			$page = $tbCurrentPage->selectPageByCode($_GET['page']);
			if(count($page) != 0)
			{
				$this->view->md5_code = $_GET['page'];
				$title_DB = unserialize($page[0]['title']);
			}
			else
			{
				$this->_redirect("/admin");
			}
		}
        if ((isset($_POST)) && (count($_POST) > 0) && (isset($_GET['action'])) && ($_GET['action'] == "new")) 
        {
        	$error_title = array();
            if (! isset($_POST['title']['Default']) || (strlen($_POST['title']['Default']) < 5)) 
            {
               $error_title['Default'] = 1;
            }
            foreach ($Languages as $lang)
            {
              	if (!isset($_POST['title'][$lang['name']]) || (strlen($_POST['title'][$lang['name']]) < 5)) 
            	{
               		 $error_title[$lang['name']] = 1;
           	 	}
            }
            if (isset($_POST['code']) && (strlen($_POST['code']) > 5)) 
            {
                if(!isset($_GET['page'])||(!isset($page))||($page[0]['md5_code']!=$_GET['page']))
                {
                	$md5 = md5($_POST['code']);
               	 	$page_temp = $tbCurrentPage->selectPageByCode($md5);
               		if (count($page_temp) > 0)  $this->view->error = 3;
                }
                
            } 
            else 
            {
                $this->view->error = 4;
            }
            $this->view->error_title = $error_title;
            if (!isset($this->view->error)&&($error_title==array())) 
            {
            	$title = array();
            	$description = array();
            	$description_table = array();
            	$title["Default"] = $_POST['title']["Default"];
            	$description["Default"] = "";
            	$description_table["Default"] = "";
            	foreach ($Languages as $lang)
            	{
            		$title[$lang['name']] = $_POST['title'][$lang['name']];
            		$description[$lang['name']] = "";
            		$description_table[$lang['name']] = "";
            	}
            	$data = array(
                		'title' => serialize($title), 
                		'page_code' => $_POST['code'], 
                		'md5_code' => md5($_POST['code']), 
                		'description' => serialize($description),
                		'description_table' => serialize($description_table),
                		'link' => $_SERVER['HTTP_HOST'] . '/newpage?page=' .md5($_POST['code']), 
                		'updated_date' => Util::get_time_now()
                );
                unset($_SESSION['edit_page']);
				unset($_SESSION['description_page']);
				unset($_SESSION['description_table_page']);
				unset($_SESSION['item']);
				unset($_SESSION['step_edit']);
            	if(isset($_GET['page']))
            	{
            		$page = $tbCurrentPage->selectPageByCode($_GET['page']);
            		if(count(page)<=0)
            		{
            			$this->_redirect("/admin");
            		}
            		else
            		{
            			$_SESSION['array_page_data'] = $data;
            			$this->_redirect("/admin/editor?item=page&step=0&page=".md5($_POST['code']));
            		}
            	}
            	else
            	{
            		$data['default'] = 0;
            		$data['created_date'] = 0;
            		$_SESSION['array_page_data'] = $data;
					$this->_redirect("/admin/editor?item=page&step=0");
            	}
            }
        }
        $post_title = array();
        if (isset($_POST["title"]['Default'])) 
        {
            $post_title['Default'] = $_POST["title"]["Default"];
        } 
        else if (isset($title_DB))
        {
        	$post_title['Default'] = $title_DB['Default'];
        }
        else 
        {
           $post_title['Default']="";
        }
         foreach ($Languages as $lang){
      	 	if (isset($_POST["title"][$lang['name']])) 
      	 	{
            	$post_title[$lang['name']] = $_POST["title"][$lang['name']];
        	} 
          	else if (isset($title_DB))
       	 	{
        		$post_title[$lang['name']] = $title_DB[$lang['name']];
       		}
         	else 
         	{
            	$post_title[$lang['name']] = "";
         	}
         }
         $this->view->title = $post_title;
        if (isset($_POST["code"])) {
            $this->view->code = $_POST["code"];
        } 
		else if (isset($page)){
        		$this->view->code =$page[0]['page_code'];
       	}
        else {
            $this->view->code = "";
        }
	}
	
	public function deleteAction(){
		if(isset($_GET['page']))
		{
			$tbCurrentPage = new Admin_Model_CurrentPage();
			$page = $tbCurrentPage->selectPageByCode($_GET['page']);
			if((count($page)>0)&&($page[0]['default']==0))
			{
				$tbContentModuleToPage = new Admin_Model_ContentModuleToPage();
				$tbCurrentPage->deleteCurrentPage($page[0]['page_code']);
				$tbContentModuleToPage->deleteAllModuleByPageCode($page[0]['page_code']);
				$this->_redirect("/admin/page");
			}
			else{
				$this->_redirect("/admin/page");
			}
		}
		else{
			$this->_redirect("/admin/page");
		}
	}
}